/**
 *  Copyright (C) 2008 Richard Hall
 *
 *  This program is free software; you can redistribute it and/or modify it under the terms of the 
 *  GNU General Public License as published by the Free Software Foundation; either version 3 of the 
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
 *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along with this program; if not, 
 *  see <http://www.gnu.org/licenses>.
 *
 *  Additional permission under GNU GPL version 3 section 7
 *
 *  If you modify this Program, or any covered work, by linking or combining it with [name of library] 
 *  (or a modified version of that library), containing parts covered by the terms of 
 *  [name of library's license], the licensors of this Program grant you additional permission to 
 *  convey the resulting work. {Corresponding Source for a non-source form of such a combination shall 
 *  include the source code for the parts of [name of library] used as well as that of the 
 *  covered work.}
 *
 * 
 */

package com.gpsmedia.gui.frames;

import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;

import com.gpsmedia.dataobject.NmeaData;

/**
 * This class is used to select the picture data to be included. It allows viewing
 * of the picture and setting of various parameters.
 * 
 * @author Richard Hall
 */
public class PictureFrame extends javax.swing.JFrame {

    private LinkedHashMap data;
    private NmeaData datarow;
    private NmeaData tempData;
    private Collection col;
    private Iterator it;
    private int currentPic;
    private ArrayList logoList = new ArrayList();
    private MainFrame frame = null;

    /**
	 * Constructor which takes a LinkedHashMap containing NmeaData objects
	 * 
	 * @param data
	 *            LinkedHasMap of NmeaData objects
         * @param showFrame
         *            Boolean true if fram to be shown, false if not
         * @param mainFrame Put in as a work around to a problem with user access to the main frame. Can be set to null
         * if being used as part of the API.     
         */
    public PictureFrame(LinkedHashMap data, boolean showFrame, MainFrame mainFrame) {
        
        if(mainFrame!=null){
            frame = mainFrame;
            frame.setVisible(false);
        }
        ClassLoader loader = getClass().getClassLoader();
        URL fileLocation = loader.getResource("com/gpsmedia/images/earth_small.jpg");
        Image logo = Toolkit.getDefaultToolkit().getImage(fileLocation);
        logoList.add(logo);


        this.data = data;

        col = data.values();
        it = col.iterator();
        currentPic = -1;
        tempData = null;

        if (showFrame) {
            datarow = findNextPic();

            if (datarow != null) {

                initComponents();
                this.setVisible(true);
            } else {
                if(frame!=null){
                    frame.setVisible(true);
                }
                dispose();
            }

        } else {
            datarow = findNextPic();
            while (datarow != null) {
                datarow.getPictures()[currentPic].setChecked(true);
                datarow.getPictures()[currentPic].setInclude(true);
                datarow.getPictures()[currentPic].setTitle(datarow.getDate().getTime().toString());
                datarow.getPictures()[currentPic].setDescription("");
                data.put(datarow.getDate(), datarow);
                datarow = findNextPic();
            }
            
            dispose();

        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnAdd = new javax.swing.JButton();
        btnRemove = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txtTitle = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDesc = new javax.swing.JTextArea();
        imagePanel1 = new com.gpsmedia.gui.controls.ImagePanel(datarow.getPictures()[currentPic].getPath());

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setIconImages(logoList);
        setResizable(false);

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnRemove.setText("Remove");
        btnRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveActionPerformed(evt);
            }
        });

        jLabel1.setText("Picture Title");

        txtTitle.setText(datarow.getDate().getTime().toString());

        jLabel2.setText("Description");

        txtDesc.setColumns(20);
        txtDesc.setRows(5);
        jScrollPane1.setViewportView(txtDesc);

        javax.swing.GroupLayout imagePanel1Layout = new javax.swing.GroupLayout(imagePanel1);
        imagePanel1.setLayout(imagePanel1Layout);
        imagePanel1Layout.setHorizontalGroup(
            imagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 347, Short.MAX_VALUE)
        );
        imagePanel1Layout.setVerticalGroup(
            imagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 232, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 309, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(imagePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnAdd)
                        .addGap(27, 27, 27)
                        .addComponent(btnRemove)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(imagePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd)
                    .addComponent(btnRemove))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
	 * Button to add the picture
	 * 
	 * @param evt
	 */
    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        datarow.getPictures()[currentPic].setChecked(true);
        datarow.getPictures()[currentPic].setInclude(true);
        datarow.getPictures()[currentPic].setTitle(txtTitle.getText());
        datarow.getPictures()[currentPic].setDescription(txtDesc.getText());
        txtTitle.setText("");
        txtDesc.setText("");
        data.put(datarow.getDate(), datarow);
        datarow = null;

        datarow = findNextPic();

        if (datarow != null) {
            this.imagePanel1.changeImage(datarow.getPictures()[currentPic].getPath());
            this.txtTitle.setText(datarow.getDate().getTime().toString());
            repaint();

        } else {
            if(frame!=null){
                frame.setVisible(true);
            }
            //frame = null;
            this.imagePanel1.killImage();
            this.dispose();
        }
        
        
}//GEN-LAST:event_btnAddActionPerformed

    /**
	 * Button to remove the picture from being included
	 * 
	 * @param evt
	 */
    private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveActionPerformed
        // TODO add your handling code here:
        datarow.getPictures()[currentPic].setChecked(true);
        datarow.getPictures()[currentPic].setInclude(false);
        txtTitle.setText("");
        txtDesc.setText("");
        data.put(datarow.getDate(), datarow);
        datarow = null;
        datarow = findNextPic();

        if (datarow != null) {
            this.imagePanel1.changeImage(datarow.getPictures()[currentPic].getPath());
            this.txtTitle.setText(datarow.getDate().getTime().toString());
            repaint();
        } else {
            if(frame!=null){
                frame.setVisible(true);
            }
            //frame=null;
            this.imagePanel1.killImage();
            this.dispose();
        }
    }//GEN-LAST:event_btnRemoveActionPerformed

    /**
	 * Returns the NmeaData object for the next picture to be checked.
	 * 
	 * @return NmeaData object
	 */
    public NmeaData findNextPic() {
        boolean found = false;
        while (it.hasNext() && found == false) {
            if (currentPic == -1) {
                tempData = (NmeaData) it.next();
            }
            for (int n = currentPic + 1; n < 10; n++) {
                System.out.println(n);
                if (tempData.getPictures()[n].getPath().compareToIgnoreCase("") != 0 &&
                        tempData.getPictures()[n].isChecked() == false) {
                    currentPic = n;
                    n = 10;
                    found = true;
                }

            }
            if (found != true) {


                currentPic = -1;
                tempData = null;

            }
        }




        return tempData;

    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnRemove;
    private com.gpsmedia.gui.controls.ImagePanel imagePanel1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txtDesc;
    private javax.swing.JTextField txtTitle;
    // End of variables declaration//GEN-END:variables

    
}
